(function(){


    $(document).on('click','button',function(e){
        e.stopPropagation();
        if ($(this).attr('id') == 'btn-undo'){
            commandManager.undo();
        }else if ($(this).attr('id') == 'btn-redo'){
            commandManager.redo();
        }else if ($(this).attr('id') == 'btn-print'){
            paper.print();
        }else if ($(this).attr('id') == 'btn-clear'){
            graph.clear();
        }else if ($(this).attr('id') == 'btn-svg'){
           paper.openAsSVG();

        }else if ($(this).attr('id') == 'btn-png'){
            paper.openAsPNG();
        }else if ($(this).attr('id') == 'btn-svg'){
            paper.openAsSVG();
        }else if ($(this).attr('id') == 'btn-save'){


         //   console.log(graph.getElements().length === 1);
            var isEveryLink =   graph.getElements().every(function (v) {
               return graph.getConnectedLinks(v).length
            });
            
            
         if(isEveryLink || graph.getElements().length === 1){
        	 saveProcess();
         }else {
             layer.confirm('存在步骤未连线，是否保存',function(index){
            	 saveProcess();
            	 layer.close(index);
             })
         }

        }else if ($(this).attr('id') == 'btn-layout'){
            $.ajax({
                url : requestUrl+'/rest/webpage/render/fullPosition',
                type: 'POST',
                dataType: 'JSON',
                data : {"dsId":dsId,"modelId":modelId, "rawType":rawType},
                success: function (data) {
                    $.each(data.object.dataObjects,function(i,v){
                        var cell = graph.getCell(v.objectId);

                        cell.position(v.positionVo.positionX,v.positionVo.positionY)  //移动元素的位置
                    })

                },
                error : function(data){
                    console.log(data)
                }
            })
        }
    });

    function saveProcess(){
    	
    	   var tuid = $('.tab-pane.active', window.parent.document).attr('data-tuid');
           //       console.log($('.tab-pane.active', window.parent.document).attr('data-tuid'));
                  console.log(graph.toJSON());

                  var json = graph.toJSON();
                  var stepsList = [];
                  var linksList = [];
                  $.each(json.cells,function (i, v) {

                      if(v.type == 'basic.Image'){

                         var stepsObj = {
                              stepId : v.attrs.image.stepId,
                              tuId : tuid,
                              stepTypeCode : v.attrs.image.class,
                              taskObjectId : v.attrs.image.taskObjectId,
                              positionX : v.position.x,
                              positionY : v.position.y
                          };
                         console.log(stepsObj)
                          stepsList.push(stepsObj)
                      }else if(v.type == 'standard.Link'){
                    	
                        var  linksObj = {
                              tuId : tuid,
                              fromStepId : v.source.id,
                              endStepId : v.target.id
                          };
                        console.log(linksObj)
                          linksList.push(linksObj);
                      }
                  });
              //    console.log(JSON.stringify(stepsList),JSON.stringify(linksList));
                  $.ajax({
                      url :base + '/rest/tuLayout/saveGraphStepsAndLinks',
                      type : 'POST',
                      dataType: 'JSON',
                      data : {"tuId":tuid,"steps":JSON.stringify(stepsList),"links":JSON.stringify(linksList)},
                      success : function (result) {
                          console.log(result);
                          if(result.code !== 0){
                              layer.alert(result.msg);
                          }else {
                              layer.msg('保存成功')
                          }
                      },
                      error : function(){
                          layer.open({
                              type :1,
                              title : '提示',
                              content : '保存失败'
                          })
                      }
                  })
                  
    }
    
    function len(s) {
        var l = 0;
        var a = s.split("");
        for (var i=0;i<a.length;i++) {
            if (a[i].charCodeAt(0)<299) {
                l++;
            } else {
                l+=2;
            }
        }
        return l;
    }


})();








